print("hello")

local a = 100
print(a)

printu("utf-8中文")
printw(UToW("宽字符1"))
printw(L"宽字符2")
printa(S"窄字符")
printt(T"自动宽窄字符")

print(L"乱码")
printa("乱码")
printt("乱码")
-------------------------------------------
print("-----------------------------------")
print("GetInstance:" .. tostring(SD.GetInstance()))
printt(T"GetInstancePath:" .. SD.GetInstancePath())
printt(T"GetCurrentPath:" .. SD.GetCurrentPath())
print("GetResourceDll:" .. tostring(SD.GetResourceDll()))
printt(T"GetResourcePath:" .. SD.GetResourcePath())
printt(T"GetResourceZip:" .. SD.GetResourceZip())
printt(T"GetResourceZipPwd:" .. SD.GetResourceZipPwd())
print("GetResourceZipHandle:" .. tostring(SD.GetResourceZipHandle()))
print("GetResourceType:" .. tostring(SD.GetResourceType()))
print("-----------------------------------")
-------------------------------------------
function var_dump(var)
    if type(var) == "table" then
        print("table:{")
        for k,v in pairs(var) do
            printt(T"    " .. T(k) .. T"=" .. T(v) .. T",")
        end
        print("}")
    elseif var then
        printt(T(var))
    else
        print("nil")
    end
end
-------------------------------------------
local function onKeyTPressed(wnd)
    print("onKeyTPressed")
    var_dump(wnd:GetRoundCorner())
    print("GetDefaultDisabledColor:" .. wnd:GetDefaultDisabledColor())
    local ctrl = wnd:FindControl(T"closebtn")
    if ctrl then
        printt(T"GetName:" .. ctrl:GetName())
        printt(T"GetCursor:" .. T(ctrl:GetCursor()))
        printt(T"GetShortcut:" .. ctrl:GetShortcut())
        printt(T"GetUserData:" .. ctrl:GetUserData())
        printt(T"GetTag:" .. T(ctrl:GetTag()))
    end

end
-------------------------------------------
local function OnCreate(wnd)
    wnd:BuildWindow(T"testlua.xml")
    print("OnCreate")

    local ctrl = wnd:FindControl(T"closebtn")
    if ctrl then
        printt(T"FindControl(\"closebtn\")->GetName:" .. ctrl:GetName())
        ctrl:SetEvent("click", function(wnd, ctrl, data1, data2)
            printt(T"DoEvent:" .. ctrl:GetName() .. T".data1:" .. UToT(data1) .. T".data2:" .. T(data2))
            wnd:Close()
        end)

        ctrl:SetShortcut(T'Q')
        ctrl:SetUserData(T"userdata")
        ctrl:SetTag(1)
    end
end

local function OnZoomed(wnd, zoomed)
    print("OnZoomed")
    print(zoomed)
end

local function OnCreateControl(wnd, name)
    print("OnCreateControl")
    print(name)
end

local function OnClose(wnd,data)
    print("OnClose:" .. tostring(data))
end

local function OnDestroy(wnd)
    print("OnDestroy")
end

local maxed = false
local function OnKeyDown(wnd,key,code)
    print("OnKeyDown:" .. tostring(key) .. "," .. tostring(code))

    --[[if key==81 then
        wnd:Close()
    end--]]

    if key==84 then
        onKeyTPressed(wnd)
    end

    if key==77 then
        if maxed then
            wnd:Restore()
        else
            wnd:Maximize()
        end
        maxed = not maxed
    end

    if key==67 then
        local num = SD.WindowsCount()
        print("windows count : " .. tostring(num))

        local fwnd = SD.FindWindow(T"testlua")
        if fwnd then
            print("find window")
            if fwnd == wnd then
                print("find window = wnd")
            end
        end
    end

    if key==80 then
        print("GetStyle:" .. tostring(wnd:GetStyle()))
    end

    
end

local function OnKeyUp(wnd,key,code)
    print("OnKeyUp")
end

local function OnSetFocus(wnd)
    print("OnSetFocus")
end

local function OnKillFocus(wnd)
    print("OnKillFocus")
end

-------------------------------------------
local wnd = SD.NewWindow(T"testlua")
if wnd then
    wnd:SetEvent("OnCreate", OnCreate)
    wnd:SetEvent("OnZoomed", OnZoomed)
    wnd:SetEvent("OnCreateControl", OnCreateControl)
    wnd:SetEvent("OnClose", OnClose)
    wnd:SetEvent("OnDestroy", OnDestroy)
    wnd:SetEvent("OnKeyDown", OnKeyDown)
    wnd:SetEvent("OnKeyUp", OnKeyUp)
    wnd:SetEvent("OnSetFocus", OnSetFocus)
    wnd:SetEvent("OnKillFocus", OnKillFocus)

    local wndparam = {
        title = T"title",
        style= ED_FRAME,
        exStyle = ED_EX_TOPMOST | ED_EX_TOOLTIP
    }
    if wnd:Create(wndparam) then
        wnd:CenterWindow()
        wnd:ShowWindow()
    end
end